<?php
header('Conect-type:text/html;charset=utf8');
//开启Session会话
Session_start();
//引入配置文件
	include('../public/config.php');
	//var_dump($_GET['a']);
		//开启数据库连接，若连接失败则返回错误信息
		$con=@mysql_connect(HOST,USER,PASS) or die("数据库连接失败!");
		//选择数据库，并设置字符集
		mysql_select_db(DBNAME,$con);
		mysql_set_charset('utf8');
		
	switch($_GET['a']){
		case "doLogin":
			//判断账号密码是否为空
			if($_POST['username']=='' || $_POST['pwd']==''||$_SESSION=''){
			echo "<script>alert('账号密码不能为空!');window.location.href='login.php';</script>";
			}
			 //编写sql语句
			 $sql="select username,pass,state from users where username='{$_POST['username']}'";
			 //执行sql语句，并返回结果集
			 $result=mysql_query($sql);
			 //判断输入的姓名是否存在
			 if($result){
			 //遍历结果集并判断是账号、密码是否正确
			 $row=mysql_fetch_assoc($result);
				 if($_POST['username'] == $row['username']&&$_POST['pwd']==$row['pass']&&$row['state']==0){
					$_SESSION['userlogin']=$row;
					echo"<script>alert('登录成功！');window.location.href='./index.php';</script>";
				 }else{
					echo "<script>alert('登录失败！');window.location.href='./login.php';</script>";
				 }
				
				}else{
					echo "<script>alert('账号密码不存在！');window.location.href='./login.php';</script>";
				}
				//销毁结果集
				mysql_free_result($result);
				break;
				
				case 'doAdd':
					//接收post传递的值
					$info['username']=$_POST['username'];
					$info['name']=$_POST['name'];
					$info['pass']=$_POST['pass'];
					$info['repass']=$_POST['repass'];
					$info['sex']=$_POST['sex'];
					$info['address']=$_POST['address'];
					$info['code']=$_POST['code'];
					$info['phone']=$_POST['phone'];
					$info['email']=$_POST['email'];
					$info['state']=$_POST['state'];
					$info['addtime']=time();
					//判断接收的值
					if($info['username']==''){
						echo '账号不能为空';
						exit;
					}elseif($info['pass']==''||$info['repass']==''){
						echo '密码不能为空';
						exit;
					}elseif($info['pass']!=$info['repass']){
						echo '两次密码不相同，请重新输入！';
						exit;
					}elseif($info['name']==''){
						echo '姓名不能为空！';
						exit;
					}
					//编写sql语句
					$sql="insert into users values(null,'{$info['username']}','{$info['name']}','{$info['pass']}',{$info['sex']},'{$info['address']}','{$info['code']}','{$info['phone']}','{$info['email']}',{$info['state']},{$info['addtime']})";
					//执行sql语句并返回结果集
					mysql_query($sql,$con);
					//解析结果集并判断操作是否执行成功
					if((mysql_insert_id($con))>0){
						echo "<script>alert('添加成功！');window.location.href='./userlist.php';</script>";
					}else{
						echo "<script>alert('添加失败！');window.location.href='./index.php';</script>";
					}
					$_SESSION['error'];
					break;
					
					case 'doupdate':
						//接收传过来的值
						$id=$_GET['id'];
						$info['username']=$_POST['username'];
						$info['name']=$_POST['name'];
						$info['pass']=$_POST['pass'];
						$info['repass']=$_POST['repass'];
						$info['sex']=$_POST['sex'];
						$info['address']=$_POST['address'];
						$info['code']=$_POST['code'];
						$info['phone']=$_POST['phone'];
						$info['email']=$_POST['email'];
						$info['state']=$_POST['state'];
						//var_dump($info);
						//编写sql语句
						$usql="update users set username='{$info['username']}',name='{$info['name']}',pass='{$info['pass']}',sex={$info['sex']},address='{$info['address']}',code='{$info['code']}',phone='{$info['phone']}',email='{$info['email']}',state={$info['state']} where id={$_GET['id']}";
						//执行sql语句
						//echo $usql;
						mysql_query($usql);

						//判断是否执行成功
						if(mysql_affected_rows($con)>0){
							echo "<script>alert('修改成功！');window.location.href='./userlist.php';</script>";
						}else{
							echo "<script>alert('修改失败！');window.location.href='./userlist.php';</script>";
						}
						
					break;
					
					case "doDel":
					//接收传值
					$id=$_GET['id'];
					//编写sql语句
					$dsql="delete from users where id={$id}";
					//执行sql语句
					mysql_query($dsql);
					//判断执行是否成功
					if(mysql_affected_rows($con)>0){
						echo '执行成功！';
					}else{
						echo '删除失败！';
					}
					break;
				//关闭连接
				mysql_close($con);
				case "doUnset":
				 //销毁SESSION
				 unset($_SESSION['userlogin']);
				 header('location:login.php');
				 break;
			}
